package flathub

import (
	"encoding/json"

	"gitee.com/zinface/go.FlatpackManager/utils"
)

// https://www.flathub.org/api/v1/apps

const (
	flathub = "https://www.flathub.org/api/v1/apps"

	categoryAudioVideo  = "https://www.flathub.org/api/v1/apps/category/AudioVideo"
	categoryDevelopment = "https://www.flathub.org/api/v1/apps/category/Development"
	categoryEducation   = "https://www.flathub.org/api/v1/apps/category/Education"
	categoryGame        = "https://www.flathub.org/api/v1/apps/category/Game"
	categoryGraphics    = "https://www.flathub.org/api/v1/apps/category/Graphics"
	categoryNetwork     = "https://www.flathub.org/api/v1/apps/category/Network"
	categoryOffice      = "https://www.flathub.org/api/v1/apps/category/Office"
	categoryScience     = "https://www.flathub.org/api/v1/apps/category/Science"
	categorySystem      = "https://www.flathub.org/api/v1/apps/category/System"
	categoryUtility     = "https://www.flathub.org/api/v1/apps/category/Utility"
)

type Package struct {
	FlatpakAppId          string `json="flatpakAppId"`          // flatpak 应用 id
	Name                  string `json="name"`                  // 应用名称
	Summary               string `json="summary"`               // 应用说明
	CurrentReleaseVersion string `json="currentReleaseVersion"` // 应用版本
	CurrentReleaseDate    string `json="currentReleaseDate"`    // 应用日期
	IconDesktopUrl        string `json="iconDesktopUrl"`        // 应用图标(桌面)
	IconMobileUrl         string `json="iconMobileUrl"`         // 应用图标(移动)
	InStoreSinceDate      string `json="inStoreSinceDate"`      // 建立时间
}

type Packages []Package

type FlatHub struct {
	All         Packages
	AudioVideo  Packages
	Development Packages
	Education   Packages
	Game        Packages
	Graphics    Packages
	Network     Packages
	Office      Packages
	Science     Packages
	System      Packages
	Utility     Packages
}

func GetPackagess(url string) Packages {
	var data = utils.GetHttpData(url)

	var packages Packages

	json.Unmarshal(data, &packages)

	return packages
}

func GetcategoryAll() Packages {
	return GetPackagess(flathub)
}
func GetcategoryAudioVideo() Packages {
	return GetPackagess(categoryAudioVideo)
}
func GetcategoryDevelopment() Packages {
	return GetPackagess(categoryDevelopment)
}
func GetcategoryEducation() Packages {
	return GetPackagess(categoryEducation)
}
func GetcategoryGame() Packages {
	return GetPackagess(categoryGame)
}
func GetcategoryGraphics() Packages {
	return GetPackagess(categoryGraphics)
}
func GetcategoryNetwork() Packages {
	return GetPackagess(categoryNetwork)
}
func GetcategoryOffice() Packages {
	return GetPackagess(categoryOffice)
}
func GetcategoryScience() Packages {
	return GetPackagess(categoryScience)
}
func GetcategorySystem() Packages {
	return GetPackagess(categorySystem)
}
func GetcategoryUtility() Packages {
	return GetPackagess(categoryUtility)
}

func NewFlatHubPackages() *FlatHub {
	var FlatHubPackages = &FlatHub{}

	FlatHubPackages.All = GetcategoryAll()
	FlatHubPackages.AudioVideo = GetcategoryAudioVideo()
	FlatHubPackages.Development = GetcategoryDevelopment()
	FlatHubPackages.Education = GetcategoryEducation()
	FlatHubPackages.Game = GetcategoryGame()
	FlatHubPackages.Graphics = GetcategoryGraphics()
	FlatHubPackages.Network = GetcategoryNetwork()
	FlatHubPackages.Office = GetcategoryOffice()
	FlatHubPackages.Science = GetcategoryScience()
	FlatHubPackages.System = GetcategorySystem()
	FlatHubPackages.Utility = GetcategoryUtility()
	return FlatHubPackages
}
